<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/26
 * Time: 13:55
 */

class product_sap extends MY_Controller{
    function __construct()
    {
        parent::__construct();
        //加载模型
        $this->load->model('product_sap_model','sapModel');
    }

    /**
     * 站点列表
     */
    public function SiteProductList(){
        $siteList[0]=array('id'=>1,'name'=>'ES');
        $siteList[1]=array('id'=>2,'name'=>'UK');
        $siteList[2]=array('id'=>3,'name'=>'DE');
        $siteList[3]=array('id'=>4,'name'=>'RU');
        $siteList[4]=array('id'=>5,'name'=>'US');
        $siteList[5]=array('id'=>6,'name'=>'OMS');
        $siteList[6]=array('id'=>7,'name'=>'FR');
        $siteList[7]=array('id'=>8,'name'=>'SZ');
        //$siteList[6]=array('id'=>7,'name'=>'ETA');
        $data['siteList']=$siteList;
        $data['permission']=$this->permission;
        $data['sapProductList']=$this->sapModel->SapProductList();
        $this->load->view('defaults/sap/index',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 站点产品列表
     * @param string $site
     */
    public function SiteTypeList($site=''){
        $gp=gp();
        $siteId=$site?$site:$gp['site'];
        $data['typeList']=$this->sapModel->GetSiteProductType($siteId);
        $data['siteISO']=$this->GetSiteISO($siteId);
        $data['siteId']=$siteId;
        $data['permission']=$this->permission;
        $this->load->view('defaults/sap/type',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 站点品牌列表
     * @param string $site
     * @param string $tid
     * @param string $name
     * @param string $custom_id
     */
    public function SiteBrandList($site='',$tid='',$name='',$custom_id=''){
        $gp=gp();
        $siteId=$site?$site:$gp['site'];
        $typeId=$tid?$tid:$gp['tid'];
        $nameText=$name?$name:$gp['name'];
        $custom_id_val=$custom_id?$custom_id:$gp['custom_id'];
        $data['brandList']=$this->sapModel->GetSiteProductBrand($siteId,$typeId,$custom_id_val);
        $data['length']=ceil(count($data['brandList'])/4);
        $data['siteISO']=$this->GetSiteISO($siteId);
        $data['siteId']=$siteId;
        $data['name']=$nameText;
        $data['permission']=$this->permission;
        //查询父级是否已经被推送(未推送，子级不可推)
        $whereArray = array('site' => $siteId, 'tid' => $typeId);
        if($custom_id_val){
            $whereArray['custom_id']=$custom_id_val;
        }
        $site=$this->sapModel->QueryParentIs_sync($whereArray,'site_product_type');
        if($site==0){
            $data['disabled']='disabled';
        }
        $this->load->view('defaults/sap/brand',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 站点型号列表
     * @param string $site
     * @param string $bid
     * @param string $name
     * @param string $tid
     * @param string $custom_id
     */
    public function SiteModelList($site='',$bid='',$name='',$tid='',$custom_id=''){
        $gp=gp();
        $siteId=$site?$site:$gp['site'];
        $brandId=$bid?$bid:$gp['bid'];
        $nameText=$name?$name:$gp['name'];
        $typeId=$tid?$tid:$gp['tid'];
        $custom_id_val=$custom_id?$custom_id:$gp['custom_id'];
        //型号列表
        $data['modelList']=$this->sapModel->GetSiteProductModel($siteId,$brandId,$typeId,$custom_id_val);
        //系列列表
        $data['seriesList']=$this->sapModel->GetSiteSeriesList($siteId,$brandId);
        $data['siteISO']=$this->GetSiteISO($siteId);
        $data['custom_id']=$custom_id_val;
        $data['siteId']=$siteId;
        $data['name']=$nameText;
        $data['tid']=$typeId;
        $data['permission']=$this->permission;
        //查询父级是否已经被推送(未推送，子级不可推)
        $whereArray=array('site'=>$siteId,'tid'=>$typeId,'bid'=>$brandId);
        if($custom_id_val){
            $whereArray['custom_id']=$custom_id_val;
        }
        $site=$this->sapModel->QueryParentIs_sync($whereArray,'site_product_brand');
        if($site==0){
            $data['disabled']='disabled';
        }
        $this->load->view('defaults/sap/model',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 添加产品类型
     */
    public function AddSiteProductType(){
        $gp=gp();
        foreach($gp['type'] as $key=>$val){
            $valueArray=explode('_',$val);
            //判断是否是自定义类型
            if(count($valueArray)==3) {
                $data = array(
                    'site' => $gp['site'],
                    'tid' => $valueArray[0],
                    'name' => $valueArray[1],
                    'name_en' => $valueArray[2],
                );
                $result=$this->common_model->selectFirst('site_product_type',array('site'=>$gp['site'],'tid'=>$valueArray[0],'is_custom'=>0));
                if(!$result){
                    $this->sapModel->AddSiteProductType($data);
                }
            }else{
                $data = array(
                    'site' => $gp['site'],
                    'tid' => $valueArray[0],
                    'custom_id'=>$valueArray[0].'_'.$valueArray[1],
                    'name' => $valueArray[2],
                    'name_en' => $valueArray[3],
                    'is_custom'=>1
                );
                $result=$this->common_model->selectFirst('site_product_type',array('site'=>$gp['site'],'tid'=>$valueArray[0],'custom_id'=>$valueArray[0].'_'.$valueArray[1]));
                if(!$result){
                    $this->sapModel->AddSiteProductType($data);
                }
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteTypeList?site='.$gp['site']));
    }

    /**
     * 添加品牌
     */
    public function AddSiteProductBrand(){
        $gp=gp();
        foreach($gp['brand'] as $key=>$val){
            $valueArray=explode('_',$val);
            $data=array(
                'site'=>$gp['site'],
                'tid'=>$gp['tid'],
                'bid'=>$valueArray[0],
                'custom_id'=>$gp['custom_id'],
                'name'=>$valueArray[1],
            );
            $result=$this->common_model->selectFirst('site_product_brand',array('site'=>$gp['site'],'tid'=>$gp['tid'],'bid'=>$valueArray[0],'custom_id'=>$gp['custom_id']));
            if(!$result) {
                $this->sapModel->AddSiteProductBrand($data);
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteBrandList?site='.$gp['site'].'&tid='.$gp['tid'].'&name='.$valueArray[1].'&custom_id='.$gp['custom_id']));
    }

    /**
     * 添加型号
     */
    public function AddSiteProductModel(){
        $gp=gp();
        foreach($gp['model'] as $key=>$val){
            $valueArray=explode('_',$val);
            $data=array(
                'site'=>$gp['site'],
                'custom_id'=>$gp['custom_id'],
                'tid'=>$gp['tid'],
                'bid'=>$gp['bid'],
                'mid'=>$valueArray[0],
                'name'=>$valueArray[1],
            );
            $result=$this->common_model->selectFirst('site_product_model',array('site'=>$gp['site'],'tid'=>$gp['tid'],'bid'=>$gp['bid'],'custom_id'=>$gp['custom_id'],'mid'=>$valueArray[0]));
            if(!$result) {
                $this->sapModel->AddSiteProductModel($data);
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteModelList?site='.$gp['site'].'&bid='.$gp['bid'].'&tid='.$gp['tid'].'&name='.'&custom_id='.$gp['custom_id']));
    }

    /**
     * 添加站点产品属性
     */
    public function AddSiteProductAttribute(){
        $gp=gp();
        $colorExp=explode('_',$gp['color_id']);
        $romExp=explode('_',$gp['rom_id']);
        $color_id=$colorExp[0];
        $rom_id=$romExp[0];
        $name='';
        $data=array(
            'mid'=>$gp['mid'],
            'name'=>'',
            'color_id'=>'',
            'rom_id'=>'',
            'site'=>$gp['site'],
        );
    }

    /**
     * 新增自定义类型
     */
    public function AddSiteCustomType(){
        $gp=gp();
        $data=array(
            'type_id'=>$gp['type_id'],
            'custom_id'=>'A'.$gp['type_id'],
            'name_ch'=>$gp['name_ch'],
            'name_en'=>$gp['name_en'],
            'add_name'=>$this->users['name'],
            'add_time'=>time()
        );
        $id=$this->sapModel->AddSiteCustomType($data);
        if($id){
            $this->sapModel->UpdateSiteCustomType($id,array('custom_id'=>$gp['type_id'].'_'.$id));
            redirect(blang_url('productClassify/product_sap/SiteProductList'));
        }
    }

    /**预防之后数据结构变动 三大类更新函数分开写 **/
    /**
     * 更新产品类型信息
     */
    public function UpdateTypeInfo(){
        $gp=gp();
        //更新数组
        $data=array(
            'sort_id'=>$gp['sort_id'],
            'authorization'=>$gp['authorization'],
        );
        if($gp['name'] && $gp['name_en']){
            $data['name']=trim($gp['name']);
            $data['name_en']=trim($gp['name_en']);
        }
        //LOGO存放路径
        $dir='./resource/uploadfiles/image/sap/';
        if($_FILES['logo']['name']) {
            $logo = uploadfile($_FILES['logo'], $dir);//上传LOGO至服务器
            $data['logo'] = $logo;
        }
        //更新产品类型数据
        $result=$this->sapModel->UpdateTypeInfo($gp['id'],$data);
        if($result){
            if($gp['remark']) {
                $addData = array(
                    'remark' => $gp['remark'],
                    'type_name' => $gp['type_name'],
                    'time' => date('Y/m/d',time()),
                    'add_name' => $this->users['name'],
                    'type_id' => $gp['id']
                );
                $this->sapModel->AddSiteProductRemark($addData);
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteTypeList?site='.$gp['site']));
    }

    /**
     * 更新品牌信息
     */
    public function UpdateBrandInfo(){
        $gp=gp();
        //更新数组
        $data=array(
            'sort_id'=>$gp['sort_id'],
            'authorization'=>$gp['authorization'],
        );
        //LOGO存放路径
        $dir='./resource/uploadfiles/image/sap/';
        if($_FILES['logo']['name']) {
            $logo = uploadfile($_FILES['logo'], $dir);//上传LOGO至服务器
            $data['logo'] = $logo;
        }
        //更新产品类型数据
        $result=$this->sapModel->UpdateBrandInfo($gp['id'],$data);
        if($result){
            if($gp['remark']) {
                $addData = array(
                    'remark' => $gp['remark'],
                    'type_name' => $gp['type_name'],
                    'time' => date('Y/m/d',time()),
                    'add_name' => $this->users['name'],
                    'brand_id' => $gp['id']
                );
                $this->sapModel->AddSiteProductRemark($addData);
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteBrandList?site='.$gp['site'].'&tid='.$gp['tid'].'&custom_id='.$gp['custom_id']));
    }

    /**
     * 更新型号信息
     */
    public function UpdateModelInfo(){
        $gp=gp();
        //更新数组
        $data=array(
            'sort_id'=>$gp['sort_id'],
            'authorization'=>$gp['authorization'],
            'warranty_type'=>$gp['warranty_type'],
            'series_id'=>$gp['series_id'],
        );
       /* //LOGO存放路径
        $dir='./resource/uploadfiles/image/sap/';
        if($_FILES['logo']['name']) {
            $logo = uploadfile($_FILES['logo'], $dir);//上传LOGO至服务器
            $data['logo'] = $logo;
        }*/
        //更新产品类型数据
        $result=$this->sapModel->UpdateModelInfo($gp['id'],$data);
        if($result){
            if($gp['remark']) {
                $addData = array(
                    'remark' => $gp['remark'],
                    'type_name' => $gp['type_name'],
                    'time' => date('Y/m/d',time()),
                    'add_name' => $this->users['name'],
                    'model_id' => $gp['id']
                );
                $this->sapModel->AddSiteProductRemark($addData);
            }
        }
        redirect(blang_url('productClassify/product_sap/SiteModelList?site='.$gp['site'].'&bid='.$gp['bid'].'&tid='.$gp['tid'].'&name='.$gp['name'].'&custom_id='.$gp['custom_id']));
    }

    /**
     * 获取国家ISO码
     * @param $site
     * @return string
     */
    public function GetSiteISO($site){
        switch($site){
            case '1':
                $name='ES';
                break;
            case '2':
                $name='UK';
                break;
            case '3':
                $name='DE';
                break;
            case '4':
                $name='RU';
                break;
            case '5':
                $name='US';
                break;
            case '6':
                $name='OMS';
                break;
            case '7':
                $name='FR';
                break;
            case '8':
                $name='SZ';
                break;
            default:
                $name='ES';
                break;
        }
        return $name;
    }
}